﻿// Type: OAO.Form1
// Assembly: OAO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: C41C173B-7C29-484B-93FF-6B6419899BF7
// Assembly location: C:\Users\hiroshi0916\Google Drive\OAO.beta\OAO.exe

using System.Collections;
using System.Collections.Generic;
using System.Net.Configuration;
using Gma.UserActivityMonitor;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using QAQ;

namespace OAO
{
    public class Form1 : Form
    {
        public Form1()
        {
            this.InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.radioButton10 = new System.Windows.Forms.RadioButton();
            this.radioButton9 = new System.Windows.Forms.RadioButton();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.radioButton3 = new System.Windows.Forms.RadioButton();
            this.groupBox3.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(12, 16);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(48, 20);
            this.textBox2.TabIndex = 1;
            this.textBox2.Text = "0";
            this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(66, 16);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(48, 20);
            this.textBox3.TabIndex = 7;
            this.textBox3.Text = "178";
            this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(63, 39);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(45, 13);
            this.label5.TabIndex = 8;
            this.label5.Text = "NtDelay";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(9, 39);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(57, 13);
            this.label6.TabIndex = 9;
            this.label6.Text = "FarmDelay";
            // 
            // groupBox3
            // 
            this.groupBox3.Controls.Add(this.radioButton10);
            this.groupBox3.Controls.Add(this.radioButton9);
            this.groupBox3.Location = new System.Drawing.Point(12, 75);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(122, 57);
            this.groupBox3.TabIndex = 20;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Char?";
            // 
            // radioButton10
            // 
            this.radioButton10.AutoSize = true;
            this.radioButton10.Checked = true;
            this.radioButton10.Location = new System.Drawing.Point(6, 34);
            this.radioButton10.Name = "radioButton10";
            this.radioButton10.Size = new System.Drawing.Size(41, 17);
            this.radioButton10.TabIndex = 1;
            this.radioButton10.TabStop = true;
            this.radioButton10.Text = "Dio";
            this.radioButton10.UseVisualStyleBackColor = true;
            this.radioButton10.CheckedChanged += new System.EventHandler(this.radioButton9_CheckedChanged);
            // 
            // radioButton9
            // 
            this.radioButton9.AutoSize = true;
            this.radioButton9.Location = new System.Drawing.Point(6, 17);
            this.radioButton9.Name = "radioButton9";
            this.radioButton9.Size = new System.Drawing.Size(58, 17);
            this.radioButton9.TabIndex = 0;
            this.radioButton9.Text = "Normal";
            this.radioButton9.UseVisualStyleBackColor = true;
            this.radioButton9.CheckedChanged += new System.EventHandler(this.radioButton9_CheckedChanged);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.textBox2);
            this.groupBox1.Controls.Add(this.label6);
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.textBox3);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(122, 57);
            this.groupBox1.TabIndex = 22;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Set";
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Checked = true;
            this.radioButton2.Location = new System.Drawing.Point(6, 41);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(84, 17);
            this.radioButton2.TabIndex = 11;
            this.radioButton2.TabStop = true;
            this.radioButton2.Text = "Precise (req)";
            this.radioButton2.UseVisualStyleBackColor = true;
            this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Location = new System.Drawing.Point(6, 18);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(82, 17);
            this.radioButton1.TabIndex = 10;
            this.radioButton1.Text = "Legacy (ms)";
            this.radioButton1.UseVisualStyleBackColor = true;
            this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.radioButton3);
            this.groupBox2.Controls.Add(this.radioButton1);
            this.groupBox2.Controls.Add(this.radioButton2);
            this.groupBox2.Location = new System.Drawing.Point(140, 13);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(93, 119);
            this.groupBox2.TabIndex = 23;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "groupBox2";
            // 
            // radioButton3
            // 
            this.radioButton3.AutoSize = true;
            this.radioButton3.Location = new System.Drawing.Point(6, 62);
            this.radioButton3.Name = "radioButton3";
            this.radioButton3.Size = new System.Drawing.Size(75, 17);
            this.radioButton3.TabIndex = 12;
            this.radioButton3.TabStop = true;
            this.radioButton3.Text = "Driver (ms)";
            this.radioButton3.UseVisualStyleBackColor = true;
            this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
            // 
            // Form1
            // 
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(237, 138);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.groupBox3);
            this.Name = "Form1";
            this.Text = "Form1";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.ResumeLayout(false);

        }
        private TextBox textBox2;
        private TextBox textBox3;
        private Label label5;
        private Label label6;
        private GroupBox groupBox3;
        private RadioButton radioButton10;
        private RadioButton radioButton9;
        private Thread mThread;
        private GroupBox groupBox1;
        private RadioButton radioButton2;
        private RadioButton radioButton1;


        private DxDeviceCtrl mDxDeviceCtrl;
        private JoyControl mJoyControl;

        private static bool mfarmEnabled = false;
        private static bool mStarfarmEnabled = false;
        private static bool mIsDio;

        private static int mInt32Delay1;
        private static int mInt32Delay2;
        private bool isPrecise;
        private static bool isDriver;
        private GroupBox groupBox2;
        private RadioButton radioButton3;
        private bool macroEnabled;


        private void Form1_Load(object sender, EventArgs e)
        {
            Form1.mInt32Delay2 = Convert.ToInt32(this.textBox3.Text);
            Form1.mInt32Delay1 = Convert.ToInt32(this.textBox2.Text);

            isPrecise = this.radioButton2.Checked;
            mIsDio = this.radioButton10.Checked;
            macroEnabled = false;

            mDxDeviceCtrl = new DxDeviceCtrl();
            mJoyControl=new JoyControl();


            HookManager.KeyDown += new KeyEventHandler(this.OnKeyDownListener);
            HookManager.KeyUp += new KeyEventHandler(this.OnKeyUpListener);

            this.mThread = new Thread(new ThreadStart(this.Service));
            this.mThread.IsBackground = true;
            this.mThread.Priority = ThreadPriority.Highest;
            this.mThread.Start();
        }

        private void Service()
        {
            var dz = new List<DxDeviceCtrl.DirectInputKbdKey>();
            dz.Add(DxDeviceCtrl.DirectInputKbdKey.DIK_DOWN);
            dz.Add(DxDeviceCtrl.DirectInputKbdKey.DIK_Z);

            for (; ; )
            {
                if (true == Form1.isDriver)
                {
                    if (true == Form1.mfarmEnabled)
                    {
                        if (true == Form1.mIsDio)
                        {

                            mJoyControl.Key(JoyControl.KeyZ );
                            Thread.Sleep(Form1.mInt32Delay2);

                            while (true == Form1.mfarmEnabled)
                            {
                                mJoyControl.Key(JoyControl.KeyZ | JoyControl.Key_Down);
                                Thread.Sleep(Form1.mInt32Delay2);
                            }
                        }
                        else
                        {
                            while (true == Form1.mfarmEnabled)
                            {
                                mJoyControl.Key(JoyControl.Key_Down);
                                Thread.Sleep(Form1.mInt32Delay1);
                                mJoyControl.Key(JoyControl.KeyZ );
                                Thread.Sleep(Form1.mInt32Delay2);
                            }
                        }

                    }
                    else if (true == Form1.mStarfarmEnabled)
                    {
                        while (true == Form1.mStarfarmEnabled)
                        {
                            mJoyControl.Key(JoyControl.Key_Down);
                            Thread.Sleep(50);
                            mJoyControl.Key(JoyControl.KeyZ|JoyControl.Key_Down);
                            Thread.Sleep(500);
                        }
                    }
                    else
                    {
                        Thread.Sleep(3);
                    }
                }
                else
                {

                    if (true == Form1.mfarmEnabled)
                    {
                        if (true == Form1.mIsDio)
                        {

                            mDxDeviceCtrl.Send(DxDeviceCtrl.DirectInputKbdKey.DIK_Z);
                            Thread.Sleep(Form1.mInt32Delay2);

                            while (true == Form1.mfarmEnabled)
                            {
                                mDxDeviceCtrl.Send(dz);
                                Thread.Sleep(Form1.mInt32Delay2);
                            }
                        }
                        else
                        {
                            while (true == Form1.mfarmEnabled)
                            {
                                mDxDeviceCtrl.Send(DxDeviceCtrl.DirectInputKbdKey.DIK_DOWN);
                                Thread.Sleep(Form1.mInt32Delay1);
                                mDxDeviceCtrl.Send(DxDeviceCtrl.DirectInputKbdKey.DIK_Z);
                                Thread.Sleep(Form1.mInt32Delay2);
                            }
                        }

                    }
                    else if (true == Form1.mStarfarmEnabled)
                    {
                        while (true == Form1.mStarfarmEnabled)
                        {
                            mDxDeviceCtrl.Send(DxDeviceCtrl.DirectInputKbdKey.DIK_DOWN);
                            Thread.Sleep(50);
                            mDxDeviceCtrl.Send(dz);
                            Thread.Sleep(500);
                        }
                    }
                    else
                    {
                        Thread.Sleep(3);
                    }
                }
            }
        }

        private void OnKeyDownListener(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.LWin:
                    if (false == isPrecise)
                    {
                        Form1.mStarfarmEnabled = true;
                    }
                    else
                    {
                        if (false == macroEnabled)
                        {

                            mDxDeviceCtrl.SetMacro(0,
                                Form1.mInt32Delay1, Form1.mInt32Delay2,
                                new[] { DxDeviceCtrl.DirectInputKbdKey.DIK_DOWN },
                                new[]
                                {
                                    DxDeviceCtrl.DirectInputKbdKey.DIK_Z ,
                                    DxDeviceCtrl.DirectInputKbdKey.DIK_DOWN 
                                });

                            mDxDeviceCtrl.StartMacro(0);
                            macroEnabled = true;
                        }
                    }

                    break;
                case Keys.LMenu:
                    if (false == isPrecise)
                    {
                        Form1.mfarmEnabled = true;
                    }
                    else
                    {
                        if (false == macroEnabled)
                        {
                            if (true == mIsDio)
                            {
                                mDxDeviceCtrl.SetMacro(0,
                                    Form1.mInt32Delay2, Form1.mInt32Delay2,
                                    new[] 
                                        {
                                            DxDeviceCtrl.DirectInputKbdKey.DIK_Z,
                                            DxDeviceCtrl.DirectInputKbdKey.DIK_DOWN
                                        },
                                    new[]
                                        {
                                            DxDeviceCtrl.DirectInputKbdKey.DIK_Z,
                                            DxDeviceCtrl.DirectInputKbdKey.DIK_DOWN
                                        });
                            }
                            else
                            {
                                mDxDeviceCtrl.SetMacro(0,
                                    Form1.mInt32Delay1, Form1.mInt32Delay2,
                                    new[] { DxDeviceCtrl.DirectInputKbdKey.DIK_DOWN },
                                    new[] { DxDeviceCtrl.DirectInputKbdKey.DIK_Z });
                            }
                            mDxDeviceCtrl.StartMacro(0);
                            macroEnabled = true;
                        }

                    }

                    break;
                case Keys.PageUp:
                    this.textBox3.Text = (Form1.mInt32Delay2 + 1).ToString();
                    break;
                case Keys.PageDown:
                    this.textBox3.Text = (Form1.mInt32Delay2 - 1 >= 0 ? Form1.mInt32Delay2 - 1 : 0).ToString();
                    break;
                case Keys.Home:
                    this.textBox2.Text = (Form1.mInt32Delay1 + 1).ToString();
                    break;
                case Keys.End:
                    this.textBox2.Text = (Form1.mInt32Delay1 - 1 >= 0 ? Form1.mInt32Delay1 - 1 : 0).ToString();
                    break;
                case Keys.Insert:
                    this.radioButton9.Checked = true;
                    break;
                case Keys.Delete:
                    this.radioButton10.Checked = true;
                    break;

            }
        }

        private void OnKeyUpListener(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.LWin:
                    if (false == isPrecise)
                    {
                        Form1.mStarfarmEnabled = false;
                    }
                    else
                    {
                        if (true == macroEnabled)
                        {
                            mDxDeviceCtrl.StopMacro(0);
                            macroEnabled = false;
                        }
                    }

                    break;
                case Keys.LMenu:
                    if (false == isPrecise)
                    {
                        Form1.mfarmEnabled = false;
                    }
                    else
                    {
                        if (true == macroEnabled)
                        {
                            mDxDeviceCtrl.StopMacro(0);
                            macroEnabled = false;
                        }
                    }

                    break;
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Form1.mInt32Delay1 = Convert.ToInt32(this.textBox2.Text);
                this.textBox2.BackColor = Color.White;
            }
            catch (Exception )
            {
                this.textBox2.BackColor = Color.Red;
            }

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Form1.mInt32Delay2 = Convert.ToInt32(this.textBox3.Text);
                this.textBox3.BackColor = Color.White;
            }
            catch (Exception )
            {
                this.textBox3.BackColor = Color.Red;
            }

        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.mThread.Abort();
        }

        private void radioButton9_CheckedChanged(object sender, EventArgs e)
        {
            Form1.mIsDio = radioButton10.Checked;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.isPrecise = this.radioButton2.Checked;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            Form1.isDriver = this.radioButton3.Checked;
            if (true == Form1.isDriver)
            {
                this.isPrecise = false;
            }

        }
    }
}
